from app.app import db
import json
from app.utils.models import Book
book = Book()
books_descriptions = []
books_des = []
all_titles = []
all_authors = []
all_prices = []
all_derives = []
all_descriptions = []
all_imgs = []


# 作者
def get_info():
    # 书名
    with open("../informations/titles.json", "r", encoding='utf-8') as f:
        data = f.read()
        all_titles = json.loads(data)
        # for title in all_titles:
        #     print(title)
    # 作者名
    with open("../informations/authors.json", "r", encoding='utf-8') as f:
        data = f.read()
        all_authors = json.loads(data)
        # for author in all_authors:
        #     print(author)
    # 出版社
    with open("../informations/derives.json", "r", encoding='utf-8') as f:
        data = f.read()
        all_derives = json.loads(data)
        # for derive in all_derives:
        #     print(derive)
    # 描述
    with open("../informations/descriptions.json", "r", encoding='utf-8') as f:
        data = f.read()
        all_descriptions = json.loads(data)
        # for descriptions in all_descriptions:
        #     if descriptions:
        #         print(descriptions[0])
        #     else:
        #         print("简介空")
    # 价格
    with open("../informations/prices.json", "r", encoding='utf-8') as f:
        data = f.read()
        all_prices = json.loads(data)
        # for price in all_prices:
        #     print(price)
    # 封面
    with open("../informations/book_jpgs.json", "r", encoding='utf-8') as f:
        data = f.read()
        all_imgs = json.loads(data)
        # for img in all_imgs:
        #     print(img)

    for i in range(250):
        book = Book()
        book.book_name = all_titles[i]
        book.book_author = all_authors[i]
        book.price = all_prices[i]
        book.derive = all_derives[i]
        book.photo = all_imgs[i]
        if all_descriptions[i]:
            book.desc=all_descriptions[i][0]
        else:
            book.desc = "简介略"
        db.session.add(book)
    db.session.commit()
    return True
if __name__ == '__main__':
    get_info()